#include "../buffer/buffer.h"
#include <string>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unordered_map>

namespace zh
{
    class HttpResponse
    {
    public:
        HttpResponse();
        ~HttpResponse();
        void initialize(const std::string& sourceDirectory, const std::string& path, bool isKeepAlive, int code);

        void unmapFile();
        void makeResponse(Buffer& buffer);

        char*  file();
        size_t fileLength() const;

        void errorContent(Buffer& buffer, const std::string& message);
        int  code() const;

    private:
        void addLine(Buffer& buffer);
        void addHeader(Buffer& buffer);
        void addContent(Buffer& buffer);

        void        errorHTML();
        std::string getFileType() const;

    private:
        int  m_code {-1};
        bool m_isKeepAlive {false};

        std::string m_path;
        std::string m_sourceDirectory;

        char*       m_mmapFile {nullptr};
        struct stat m_mmapFileState;

        static const std::unordered_map<std::string, std::string> s_suffixTypes;
        static const std::unordered_map<int, std::string>         s_codeStatus;
        static const std::unordered_map<int, std::string>         s_codePath;
    };
} // namespace zh